package com.rjxy.plansystemmanagement.controller.entityController;

import com.rjxy.plansystemmanagement.entity.Result.Result;
import com.rjxy.plansystemmanagement.entity.User;
import com.rjxy.plansystemmanagement.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Users")
@Slf4j
@CrossOrigin(origins = "http://localhost:8080")
public class UserController {
    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/Users",method = RequestMethod.GET)

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping
    public Result list(){
        log.info("查询用户列表");
        //调用service查询用户数据
        List<User> userList = userService.list();
        return Result.success(userList);
    }



    /**
     * 增加用户
     * @return
     */
    @PostMapping
    public Result addUser(@RequestBody User user){
        log.info("增加用户");
        userService.addUser(user);
        return Result.success();
    }

    /**
     * 根据ID删除指定用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Integer id){
        log.info("根据ID{}删除用户",id);
        userService.deleteUser(id);
        return Result.success();
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    private Result getUserById(@PathVariable Integer id){
        log.info("根据id{}查询用户",id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }


    /**
     * 更新用户
     * @param user
     * @return
     */
    @PutMapping
    public Result updateUser(@RequestBody User user){
        log.info("更新用户");
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 根据用户名查询用户
     * @param UserName
     * @return
     */
    @GetMapping("/getUser/{UserName}")
    private Result getUserByUserName(@PathVariable String UserName){
        log.info("根据id{}查询用户",UserName);
        User user = userService.getUserByUserName(UserName);
        return Result.success(user);
    }
}
